﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Profil : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        pecati__gi_detalite_za_fotografot((string)Session["username"]);
    }
    protected void lnk_edit_fname_Click(object sender, EventArgs e)
    {
        lbl_user_firstname.Visible = false;
        txt_pro_fname.Visible = true;
    }
    protected void lnk_edit_lname_Click(object sender, EventArgs e)
    {
        lbl_user_lastname.Visible = false;
        txt_pro_lname.Visible = true;
    }
    protected void lnk_edit_mail_Click(object sender, EventArgs e)
    {
        lbl_user_mail.Visible = false;
        txt_pro_mail.Visible = true;
    }
    protected void lnk_edit_pass_Click(object sender, EventArgs e)
    {
        lbl_user_password.Visible = false;
        txt_pro_pass.Visible = true;
    }
    protected void btn_p_save_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection();

        myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
        
        string sqlQuestion2 = "UPDATE Photographers SET first_name=@first_name,last_name=@last_name,e_mail=@e_mail,password=@password WHERE username=@user";
        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);

        komanda2.Parameters.AddWithValue("@user", (string)Session["username"]);
        komanda2.Parameters.AddWithValue("@first_name", txt_pro_fname.Text);
        komanda2.Parameters.AddWithValue("@last_name", txt_pro_lname.Text);
        komanda2.Parameters.AddWithValue("@e_mail", txt_pro_mail.Text);
        komanda2.Parameters.AddWithValue("@password", txt_pro_pass.Text);

        int updated = 0;
        try
        {
            myConnection.Open();
            updated = komanda2.ExecuteNonQuery();
            if (updated == 0)
            {
                lbl_details_result.Text = "no updated";
            }
            else
            {
                lbl_details_result.Text = updated.ToString() + " record updated.";

            }
        }
        catch (Exception err)
        {
            lbl_details_result.Text = "Error updating author. ";
            lbl_details_result.Text += err.Message;
        }
        finally
        {
            myConnection.Close();
        }
        pecati__gi_detalite_za_fotografot((string)Session["username"]);
        
        
    }
    protected void pecati__gi_detalite_za_fotografot(string username)
    {
        lbl_user_firstname.Visible = true;
        txt_pro_fname.Visible = false;
        lbl_user_lastname.Visible = true;
        txt_pro_lname.Visible = false;
        lbl_user_mail.Visible = true;
        txt_pro_mail.Visible = false;
        lbl_user_username.Visible = true;
        lbl_user_password.Visible = true;
        txt_pro_pass.Visible = false;
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";


        //------------------------------- GI PRIKAZUVA DETALITE ZA SLIKATA -------------------------------

        string sqlQuestion = "SELECT * FROM Photographers WHERE username=@username";

        SqlCommand komanda = new SqlCommand(sqlQuestion, myConnection);
        SqlDataReader citac;

        komanda.Parameters.AddWithValue("@username", username);



        try
        {
            myConnection.Open();
            citac = komanda.ExecuteReader();
            while (citac.Read())
            {
                lbl_user_firstname.Text = citac["first_name"].ToString();
                lbl_user_lastname.Text = citac["last_name"].ToString();
                lbl_user_mail.Text = citac["e_mail"].ToString();
                lbl_user_username.Text = citac["username"].ToString();
                lbl_user_password.Text = citac["password"].ToString();
                txt_pro_fname.Text = lbl_user_firstname.Text;
                txt_pro_lname.Text = lbl_user_lastname.Text;
                txt_pro_mail.Text = lbl_user_mail.Text;
                txt_pro_pass.Text = lbl_user_password.Text;
            }
        }
        catch (Exception err)
        {
            lbl_details_result.Text = "Error reading user!";
            lbl_details_result.Text += err.Message;
            myConnection.Close();
            return;
        }
        finally
        {
            myConnection.Close();
        }
    }
}
